<?php
namespace Fendx\Component;
use Fendx\Config;
use Fendx\Singleton;

class Component {

    public static function loadInit() {
        self::loadEntitiesConfigs();
    }

    public static function loadModuleRouters() {
        $configs = ComponentRegistrar::getConfig(ComponentRegistrar::MODULE, 'Router');
        $outConfigs = [];
        foreach ($configs as $name => $values) {
            foreach ($values as $key => $value) {
                $ckey = sprintf('%s.%s', $name, $key);
                $outConfigs[$ckey] = $value;
            }
        }
        Config::set('Routers', $outConfigs);
    }

    public static function loadEntitiesConfigs(string $name = 'EntitiesPaths') {
        $paths = ComponentRegistrar::getPaths(ComponentRegistrar::MODULE);
        $dirs = [];
        foreach ($paths as $path) {
            $dirs[] = sprintf(DATABASE_PATH . 'Entities', $path);
        }
        Config::set($name, $dirs);
    }

    public static function loadConsoleConfigs(string $name = 'Consoles') {
        $configs = ComponentRegistrar::getConfig(ComponentRegistrar::MODULE, 'Console');
        $values = [];
        foreach ($configs as $key => $config) {
            if (file_exists($config['command_path'])) {
                $values[$key] = $config;
            }
        }
        Config::set($name, $values);
    }
}
